/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Ibyte
 */
@Entity
@Table(name = "DISCIPLINA")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Disciplina.findAll", query = "SELECT d FROM Disciplina d"),
    @NamedQuery(name = "Disciplina.findByCodigo", query = "SELECT d FROM Disciplina d WHERE d.codigo = :codigo"),
    @NamedQuery(name = "Disciplina.findByNome", query = "SELECT d FROM Disciplina d WHERE d.nome = :nome")})
public class Disciplina implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "CODIGO")
    private String codigo;
    
    @Basic(optional = false)
    @Column(name = "NOME")
    private String nome;

    @Basic(optional = false)
    @Column(name = "OFERECIDA")
    private boolean oferecida;
        
    
    @Basic(optional = false)
    @Column(name = "PREREQ")
    private String prereq;
    
    @JoinTable(name = "DISCIPLINA_PREREQUISITO"
            , joinColumns = {
        @JoinColumn(name = "DISCIPLINA_CODIGO", referencedColumnName = "CODIGO")}
            , inverseJoinColumns = {
        @JoinColumn(name = "PREREQUISITO_CODIGO", referencedColumnName = "CODIGO")})
    @ManyToMany
    private List<Disciplina> prereqs;
   
    public Disciplina() {
    }

    public Disciplina(String codigo) {
        this.codigo = codigo;
        this.oferecida = false;
    }
    
  
    public Disciplina(String codigo, String nome) {
        this.codigo = codigo;
        this.nome = nome;
        this.oferecida = false;
 
    }
    public Disciplina(String codigo, String nome, String prereq) {
        this.codigo = codigo;
        this.nome = nome;
        this.oferecida = false;
        this.prereq = prereq;
 /*       this.prereqs = new ArrayList<>();
        String[] p = prereq.split("(?<!F) ");
        for (String cod : p){
            if (cod.length() > 5)
                cod = cod.substring(0, 5);
            this.prereqs.add(new Disciplina(cod));          
        }*/
    }
    
    public List<Disciplina> getPrereqs(){
        return prereqs;
    }
    
    public void setPrereqs(List<Disciplina> prereqs){
        this.prereqs = prereqs;
    }
    public String getPrereqString(){
        if (prereq == null)
            return "";
        return prereq;
    }
    
    public void setPrereqString(String prereq){
        this.prereq = prereq;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public String getNome() {
        if(nome == null)
            return "";
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public boolean getOferecimento(){
        return oferecida;
    }
    
    public void setOferecimento(boolean oferecida){
        this.oferecida = oferecida;
    }
    
    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (codigo != null ? codigo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Disciplina)) {
            return false;
        }
        Disciplina other = (Disciplina) object;
        if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
            return false;
        }
        return true;
    }

   /* @Override
    public String toString() {
        return "control.Disciplina[ codigo=" + codigo + " ]";
    }*/
    
}
